Trò chơi đua xe động vật trong UNITY Engine
- ScmlPostProcessor.cs
- Unity /
- Editor /
- Spriter2Unity /
- ThirdParty /
- Assets /
- project /
2 Copyright (c) 2014 Andrew Jones, Dario Seyb
3 Based on 'Spriter2Unity' python code by Malhavok
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 */
23 using System;
24 using System.Collections.Generic;
25 using System.IO;
26 using System.Linq;
27 using System.Text;
28 using System.Xml;
29 using UnityEditor;
30 using UnityEditorInternal;
31 using UnityEngine;
32
33 namespace Assets.ThirdParty.Spriter2Unity.Editor.Unity
34 {
35 public class ScmlPostProcessor : AssetPostprocessor
36 {
37 //HACK: Currently no known way to get the path of this script file from Unity
38 const string ASSET_PATH = "Spriter2Unity/Editor/Unity/ScmlPostProcessor.cs";
39
40 static void OnPostprocessAllAssets(
41 string[] importedAssets,
42 string[] deletedAssets,
43 string[] movedAssets,
44 string[] movedFromAssetPaths)
45 {
46 //Reimport everything if the importer itself has been modified or added
47 //.Union(deletedAssets).Union(movedAssets).Union(movedFromAssetPaths)
48 bool shouldReimportAll = importedAssets.Where(s => s.EndsWith(ASSET_PATH)).FirstOrDefault() != null;
49
50 //If we should reimport all SCML files, replace the passed in array with ALL scml project files
51 if(shouldReimportAll)
52 {
53 Debug.Log("Reimporting all SCML files in project...");
54 importedAssets = AssetDatabase.GetAllAssetPaths().Where(assetPath => assetPath.EndsWith(".scml")).ToArray();
55 }
56
57 foreach (var path in importedAssets)
58 {
59 if (!path.EndsWith(".scml"))
60 continue;
61
62 ImportScml(path);
63 }
64 }
65
66 static void ImportScml(string assetPath)
67 {
68 string folderPath = Path.GetDirectoryName(assetPath);
69
70 //Load the SCML as XML
71 var doc = new XmlDocument();
72 doc.Load(assetPath);
73
74 //Parse the SCML file
75 var scml = new Spriter.ScmlObject(doc);
76
77 //TODO: Verify that all files/folders exist
78 var pb = new PrefabBuilder();
79 foreach (var entity in scml.Entities)
80 {
81 //TODO: Settings file to customize prefab location
82 var prefabPath = Path.Combine(folderPath, entity.Name + ".prefab");
83
84 //Change to forward slash for asset database friendliness
85 prefabPath = prefabPath.Replace('\\', '/');
86
87 //Either instantiate the existing prefab or create a new one
88 GameObject go;
89 var prefabGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
90 if (prefabGo == null)
91 {
92 go = new GameObject();
93 prefabGo = PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.ConnectToPrefab);
94 }
95 else
96 {
97 go = GameObject.Instantiate(prefabGo) as GameObject;
98
99 var oldAnimator = go.GetComponent<Animator>();
100 if (oldAnimator) GameObject.DestroyImmediate(oldAnimator);
101 }
102
103 //Build the prefab based on the supplied entity
104 pb.MakePrefab(entity, go, folderPath);
105
106 var animator = go.AddComponent<Animator>();
107
108
109
110 //Add animations to prefab object
111 var anim = new AnimationBuilder();
112 var allAnimClips = anim.BuildAnimationClips(go, entity, prefabPath);
113 AssetDatabase.SaveAssets();
114
115 var animatorControllerPath = Path.ChangeExtension(prefabPath, "controller");
116 UnityEditor.Animations.AnimatorController oldController = (UnityEditor.Animations.AnimatorController)AssetDatabase.LoadAssetAtPath(animatorControllerPath, typeof (UnityEditor.Animations.AnimatorController));
117 UnityEditor.Animations.AnimatorController controller = oldController;
118
119 if (!oldController)
120 {
121 controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(animatorControllerPath);
122 foreach (var animationClip in allAnimClips)
123 {
124 if (animationClip)
125 {
126 //UnityEditor.Animations.AnimatorController.AddAnimationClipToController(controller, animationClip);
127 }
128 }
129 }
130 UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, controller);
131 go.SetActive(true);
132 //Update the prefab
133 PrefabUtility.ReplacePrefab(go, prefabGo, ReplacePrefabOptions.ConnectToPrefab);
134
135 //Add a generic avatar - because why not?
136 //TODO: May need to eventually break this into a separate class
137 // ie: if we want to look for a root motion node by naming convention
138 //var avatar = AvatarBuilder.BuildGenericAvatar(go, "");
139 //avatar.name = go.name;
140 //AssetDatabase.AddObjectToAsset(avatar, prefabPath);
141
142 GameObject.DestroyImmediate(go);
143
144 AssetDatabase.SaveAssets();
145 }
146 }
147
148 }
149 }
HACK: Currently no known way to get the path of this script file from Unity
Reimport everything if the importer itself has been modified or added
.Union(deletedAssets).Union(movedAssets).Union(movedFromAssetPaths)
If we should reimport all SCML files, replace the passed in array with ALL scml project files
Load the SCML as XML
Parse the SCML file
TODO: Verify that all filesfolders exist
TODO: Settings file to customize prefab location
Change to forward slash for asset database friendliness
Either instantiate the existing prefab or create a new one
Build the prefab based on the supplied entity
Add animations to prefab object
UnityEditor.Animations.AnimatorController.AddAnimationClipToController(controller, animationClip);
Update the prefab
Add a generic avatar - because why not?
TODO: May need to eventually break this into a separate class
ie: if we want to look for a root motion node by naming convention
var avatar = AvatarBuilder.BuildGenericAvatar(go, "");
avatar.name = go.name;
AssetDatabase.AddObjectToAsset(avatar, prefabPath);